package control.panel
{
	import events.TaskEvent;
	
	import global.GameState;
	
	import model.load.XmlLoadMessage;
	
	import view.components.UIComponents;
	import view.panel.task.TaskPanel;

	public class TaskPanelControl extends PanelControl
	{
		private var xmlLoadMsg:XmlLoadMessage;
		private var taskOperateMsg:XmlLoadMessage;
		private var dataProvider:*;
		private var currentType:int;
		
		private var gameDailyXml:XML;
		private var gameOnceXml:XML;
		
		public function TaskPanelControl()
		{
			initPanel();
			initDataProvider();
			initListener();
		}
		
		private function initListener():void
		{
			_model.taskModel.addEventListener(TaskEvent.GAME_DAILY,onGameEvent);
			_model.taskModel.addEventListener(TaskEvent.GAME_ONCE,onGameEvent);
		}
		
		private function onGameEvent(event:TaskEvent):void
		{
			if (event.type == TaskEvent.GAME_DAILY)
			{
				onGameXmlBack(event.xml,0);	
			}
			else
			{
				onGameXmlBack(event.xml,1);
			}
		}
		
		private function initDataProvider():void
		{
			dataProvider = UIComponents.getDataProvider();
		}
		
		private function initPanel():void
		{
			_view.taskPanel = new TaskPanel(this);
			
		}
		
		
		private function loadTask(type:int):void
		{
			
			if (xmlLoadMsg)
			{
				xmlLoadMsg.cancel();
			}
			
			xmlLoadMsg = new XmlLoadMessage(onDailyTaskBack);
			xmlLoadMsg.url = GameState.getServerLink() + "26&key=" + GameState.snsKey + "&type=" + type;
			xmlLoadMsg.send();
			_view.taskPanel.showLoading();
		}
		
		public function onTabSwitch(index:int):void
		{
			currentType = index;
			if (GameState.isInGameRoom)
			{
				if (index == 0)
				{
					onGameXmlBack(gameDailyXml,index);
				}
				else
				{
					onGameXmlBack(gameOnceXml,index);
				}
			}
			else
			{
				loadTask(index);
			}
			
			
		}
		
		private function onGameXmlBack(xml:XML,type:int):void
		{
			
			if (xml == null)
			{
				return;
			}
			
			if (type == 0)
			{
				gameDailyXml = xml;
			}
			else
			{
				gameOnceXml = xml;
			}
			
			if (currentType == type)
			{
				onDailyTaskBack(xml,-1);
			}
		}
		
		private function onDailyTaskBack(xml:XML,id:int):void
		{
			_view.taskPanel.hideLoading();
			dataProvider.removeAll();
			dataProvider.merge(xml);
			
			_view.taskPanel.update(dataProvider);
		}
		
		public function onTaskDrop(taskId:String):void
		{
			var msg:XmlLoadMessage = new XmlLoadMessage(onTaskDropBack);
			msg.url = GameState.getServerLink() + "28&key=" + GameState.snsKey + "&taskId=" + taskId;
			msg.send();
		}
		
		private function onTaskDropBack(xml:XML,id:int):void
		{
			var result:int = int(xml.@result);
			var name:String = String(xml.@name);
			if (result == 1)
			{
				_control.alertButPanelControl.alert("任务\"" + name + "\"放弃成功！",null);
				loadTask(currentType);
			}
			else
			{
				_control.alertButPanelControl.alert("任务\"" + name + "\"放弃失败！",null);
			}
			
		}
		
		public function onTaskSubmit(taskId:String):void
		{
			taskOperateMsg = new XmlLoadMessage(onTaskSubmitBack);
			taskOperateMsg.url = GameState.getServerLink() + "42&key=" + GameState.snsKey + "&taskId=" + taskId;
			taskOperateMsg.send();	
		}
		
		private function onTaskSubmitBack(xml:XML,id:int):void
		{
			var result:int = int(xml.@result);
			var desc:String = String(xml.@desc);
			
			if (result == 1)
			{
				
			}
			
			_control.alertButPanelControl.alert(desc,"");
			loadTask(currentType);
		}
		
		public function onTaskAccept(taskId:String):void
		{
			taskOperateMsg = new XmlLoadMessage(onTaskAcceptBack);
			taskOperateMsg.url = GameState.getServerLink() + "27&key=" + GameState.snsKey + "&taskId=" + taskId;
			taskOperateMsg.send();	
		}
		

		
		
		private function onTaskAcceptBack(xml:XML,id:int):void
		{
			var result:int = int(xml.@desc);
			var alertTxt:String;
			switch (result)
			{
				case 0:
					alertTxt = "你所接受的任务太多了，先做完或放弃以前的任务再接。";
					break;
				case 1:
					alertTxt = "接受任务成功。";
					break;
				case -1:
					alertTxt = "不能重复接受该任务。";		
					break;
				case -2:
					alertTxt = "今天每日任务已领取过。";
					break;
			}
			
			if (alertTxt)
			{
				_control.alertButPanelControl.alert(alertTxt,"");
			}
			
			loadTask(currentType);
		}
		
		public function open():void
		{
			if (GameState.isInGameRoom)
			{
				_model.netRemote.showTask();
				_view.taskPanel.switchToGame();
			}
			else
			{
				_view.taskPanel.switchToHall();
				loadTask(currentType);
			}
			
			_view.taskPanel.open();
			
			
			
		}
		
		
	}
}